package com.xiaoyu.client.ui.fragment.rank;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.adapter.discovery.RankingAdapter;
import com.xiaoyu.client.model.discovery.RankParam;
import com.xiaoyu.client.model.discovery.RankingBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.client.ui.activity.help.RankingActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.base.BaseFragment;
import com.xiaoyu.commonlib.ui.widget.NestedListView;
import com.xiaoyu.commonlib.utils.CCircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 日榜碎片页面
 * Created by jk on 2018/10/25.
 */
public class RankingFragment extends BaseFragment {
    private View mView;
    @BindView(R.id.fragment_rank_no_data_img)
    ImageView nodataImg;//暂无排行榜数据
    @BindView(R.id.fragment_rank_scroll_view)
    NestedScrollView mScrollView;
    @BindView(R.id.fragment_rank_top_three_container)
    LinearLayout mThreeContainer;
    @BindView(R.id.fragment_rank_top_ten_container)
    LinearLayout mTenContainer;
    @BindView(R.id.fragment_rank_name_txt)
    TextView curRankNameTxt;//前三榜单title
    @BindView(R.id.fragment_rank_yesterday_top_ten_title)
    TextView yesRankNameTxt;//前十榜单title
    @BindView(R.id.fragment_rank_top_one_photo)
    CCircleImageView topOnePhotoImg;//第一名头像
    @BindView(R.id.fragment_rank_top_two_photo)
    CCircleImageView topTwoPhotoImg;//第二名头像
    @BindView(R.id.fragment_rank_top_three_photo)
    CCircleImageView topThreePhotoImg;//第三名头像
    @BindView(R.id.fragment_rank_top_one_name)
    TextView topOneNameTxt;//第一名昵称
    @BindView(R.id.fragment_rank_top_two_name)
    TextView topTwoNameTxt;//第二名昵称
    @BindView(R.id.fragment_rank_top_three_name)
    TextView topThreeNameTxt;//第三名昵称
    @BindView(R.id.fragment_rank_yesterday_top_ten_list_layout)
    NestedListView rankLayout;

    private int rankType;//榜单类别 0：日榜 、1：周榜 、2：月榜
    private String[] rankName = new String[]{"day","week","month"};

    private List<RankParam> mThreeList = new ArrayList<>();//金日前三列表数据
    private List<RankParam> mTenList = new ArrayList<>();//昨日前十列表数据
    private RankingAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null){
            rankType = bundle.getInt(RankingActivity.RANKING_TYPE);
        }

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null){
            mView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_rank,container,false);
            ButterKnife.bind(this,mView);
            initView();
        }
        ViewGroup parent = (ViewGroup) mView.getParent();
        if (parent != null){
            parent.removeView(mView);
        }
        return mView;
    }

    private void initView() {
        mScrollView.setVisibility(View.GONE);
        nodataImg.setVisibility(View.VISIBLE);
        mAdapter = new RankingAdapter(getActivity(),mTenList);
        rankLayout.setAdapter(mAdapter);
        if (rankType == 0){
            curRankNameTxt.setText("今日榜单");
            yesRankNameTxt.setText("昨日前十");
        }else if (rankType == 1){
            curRankNameTxt.setText("本周榜单");
            yesRankNameTxt.setText("上周前十");
        }else if (rankType == 2){
            curRankNameTxt.setText("本月榜单");
            yesRankNameTxt.setText("上月前十");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getRankingInfo();
    }

    /**
     * 获取互动榜单信息列表
     */
    private void getRankingInfo() {
        NetworkManager.getRankingDetail(rankName[rankType], new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                parseRankingInfoSuc(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {

            }
        });
    }

    private void parseRankingInfoSuc(String result) {
        RankingBean bean = new Gson().fromJson(result,RankingBean.class);
        mThreeList.clear();
        mTenList.clear();
        mThreeList.addAll(bean.getData().getTopThreeList());
        mTenList.addAll(bean.getData().getTopTenList());
        if (mTenList.size() == 0 && mThreeList.size() == 0){
            return;
        }
        mScrollView.setVisibility(View.VISIBLE);
        nodataImg.setVisibility(View.GONE);
        if (mThreeList.size() == 0){
            mThreeContainer.setVisibility(View.GONE);
        }
        if (mTenList.size() == 0){
            mTenContainer.setVisibility(View.GONE);
        }

        mAdapter.notifyDataSetChanged();

        stuffData();
    }

    //绑定数据
    private void stuffData() {
       if (mThreeList.size() == 1){
           topOneNameTxt.setText(mThreeList.get(0).getNickname());
           Glide.with(getActivity()).load(mThreeList.get(0).getUserPhoto());
       }

       if (mThreeList.size() == 2){
           topOneNameTxt.setText(mThreeList.get(0).getNickname());
           Glide.with(getActivity()).load(mThreeList.get(0).getUserPhoto()).into(topOnePhotoImg);
           topTwoNameTxt.setText(mThreeList.get(1).getNickname());
           Glide.with(getActivity()).load(mThreeList.get(1).getUserPhoto()).into(topTwoPhotoImg);
       }

       if (mThreeList.size() == 3){
           topOneNameTxt.setText(mThreeList.get(0).getNickname());
           Glide.with(getActivity()).load(mThreeList.get(0).getUserPhoto()).into(topOnePhotoImg);
           topTwoNameTxt.setText(mThreeList.get(1).getNickname());
           Glide.with(getActivity()).load(mThreeList.get(1).getUserPhoto()).into(topTwoPhotoImg);
           topThreeNameTxt.setText(mThreeList.get(2).getNickname());
           Glide.with(getActivity()).load(mThreeList.get(2).getUserPhoto()).into(topThreePhotoImg);
       }


    }
}
